package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.TruckInfo_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class TruckInfoController extends Controller {

    private final FormFactory formFactory;
    private final TruckInfo_Repository tTruckInfoRepository;
    private final HttpExecutionContext ec;

    @Inject
    public TruckInfoController(FormFactory formFactory, TruckInfo_Repository tTruckInfoRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tTruckInfoRepository = tTruckInfoRepository;
        this.ec = ec;
    }

    public CompletionStage<Result> getTruckList() {
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();

        return tTruckInfoRepository.getTruckList(PhoneNumber).thenApplyAsync(_getTruckList -> {
            return ok(Json.toJson(_getTruckList));
        }, ec.current());
    }

    public CompletionStage<Result> addTruckNo() {
        JsonNode json = request().body().asJson();
        JsonNode DeptIds = json.findValue("DeptIds");
        JsonNode LoadCapacitys = json.findValue("LoadCapacitys");
        JsonNode PlateNos = json.findValue("PlateNos");
        JsonNode TruckTypes = json.findValue("TruckTypes");


        return tTruckInfoRepository.addTruckNo(DeptIds, LoadCapacitys, PlateNos, TruckTypes).thenApplyAsync(_getTruckList -> {
            return ok(Json.toJson(_getTruckList));
        }, ec.current());
    }

    public CompletionStage<Result> areaList() {
        JsonNode json = request().body().asJson();

        return tTruckInfoRepository.areaList().thenApplyAsync(_GetList -> {
            return ok(Json.toJson(_GetList));
        }, ec.current());
    }

    public CompletionStage<Result> truckById() {
        JsonNode json = request().body().asJson();
        Integer TruckId = json.findPath("TruckId").intValue();

        return tTruckInfoRepository.truckById(TruckId).thenApplyAsync(_truckById -> {
            return ok(Json.toJson(_truckById));
        }, ec.current());
    }

    public CompletionStage<Result> truckByDeptIds() {
        JsonNode json = request().body().asJson();
        String DeptIds = json.findPath("DeptIds").textValue();

        return tTruckInfoRepository.truckByDeptIds(DeptIds).thenApplyAsync(_truckByDeptIds -> {
            return ok(Json.toJson(_truckByDeptIds));
        }, ec.current());
    }

    public CompletionStage<Result> truckByAreaNames() {
        JsonNode json = request().body().asJson();
        String AreaNames = json.findPath("AreaNames").textValue();

        return tTruckInfoRepository.truckByAreaNames(AreaNames).thenApplyAsync(_truckByAreaNames -> {
            return ok(Json.toJson(_truckByAreaNames));
        }, ec.current());
    }

    public CompletionStage<Result> truckByAreaIds() {
        JsonNode json = request().body().asJson();
        String AreaIds = json.findPath("AreaIds").textValue();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();

        return tTruckInfoRepository.truckByAreaIds(AreaIds, PhoneNumber).thenApplyAsync(_truckByAreaIds -> {
            return ok(Json.toJson(_truckByAreaIds));
        }, ec.current());
    }

    public CompletionStage<Result> ModifyTruckType() {
        JsonNode json = request().body().asJson();
        String 	PlateNo = json.findPath("PlateNo").textValue();
        String 	type = json.findPath("type").textValue();

        return tTruckInfoRepository.ModifyTruckType(PlateNo,type).thenApplyAsync(_truckByAreaIds -> {
            return ok(Json.toJson(_truckByAreaIds));
        }, ec.current());
    }
    public CompletionStage<Result> DeleteTruck() {
        JsonNode json = request().body().asJson();
        String 	PlateNo = json.findPath("PlateNo").textValue();


        return tTruckInfoRepository.DeleteTruck(PlateNo).thenApplyAsync(_truckByAreaIds -> {
            return ok(Json.toJson(_truckByAreaIds));
        }, ec.current());
    }
}
